package lx.scrmXj;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import lx.util.HttpHelper;
import lx.util.StrUtil;

import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimerTask;

/**
 * @Description: token定时获取
 * @Author: 李星 lixing_java@163.com
 * @Date: 2020/3/14 12:52
 */
public class XjTokenTask extends TimerTask {
    // token-通讯录
//    String tokenUrlForTxl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx4a6090944c3fe511&corpsecret=7RLCVtzPdjM96IHJhS3vfQgGPavydoLJjYMc1eVhJBI";
    // token-外部联系人
//    String tokenUrlForWblxr = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx4a6090944c3fe511&corpsecret=U7r5Klxux8w-atHLbnei_TCTG3QbGRoaYnVsmD2rtFQ";
    // token-应用消息
//    String tokenUrlForYgXx = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx4a6090944c3fe511&corpsecret=UqNmlcTevuvADOioMWsYe6GO32SmVPdZvNTrtOLe6YM";

    @Override
    public void run() {
        // 删除所有token
        System.out.println(MessageFormat.format("删除token结果：{0}", Db.delete("DELETE FROM XJ_SCRM_TOKEN"))); // WHERE CATEGORY IN (1,2,3)
        // 获取指定token
//        getTokenUrlForTxl(tokenUrlForTxl); // 通讯录（CATEGORY=1）
//        getTokenUrlForWblxr(tokenUrlForWblxr); // 外部联系人（CATEGORY=2）
//        getTokenUrlForYgXx(tokenUrlForYgXx); // 应用消息（CATEGORY=3）
        getToken(1);
        getToken(2);
        getToken(3);
        getToken(4);
    }

    /*
     * @Description 获取指定类型的TOKEN
     * @Author lixing
     * @Date 2020/4/22 9:37
     * @Param [category TOKEN的类型]
     * @return void
     */
    public void getToken(int category) {
        String tokenCategory;
        String tokenParam;
        if (category == 1) {
            tokenCategory = "通讯录模块";
            tokenParam = "corpid=wx4a6090944c3fe511&corpsecret=7RLCVtzPdjM96IHJhS3vfQgGPavydoLJjYMc1eVhJBI";
        } else if (category == 2) {
            tokenCategory = "外部联系人";
            tokenParam = "corpid=wx4a6090944c3fe511&corpsecret=U7r5Klxux8w-atHLbnei_TCTG3QbGRoaYnVsmD2rtFQ";
        } else if (category == 3) {
            tokenCategory = "应用消息";
            tokenParam = "corpid=wx4a6090944c3fe511&corpsecret=UqNmlcTevuvADOioMWsYe6GO32SmVPdZvNTrtOLe6YM";
        } else if (category == 4) {
            tokenCategory = "自建应用模块(新疆电渠个人中心)";
            tokenParam = "corpid=wx4a6090944c3fe511&corpsecret=gSgjmJpxffuAGkQeqZr8FY-Fh7CKjFzUYDMu2-X-0u0";
        } else {
            tokenCategory = "TOKEN类型无效";
            tokenParam = "";
        }

        System.out.println(MessageFormat.format("getToken----{0} {1}", tokenCategory, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())));
        String result_token = HttpHelper.sendRequestHttps(
                MessageFormat.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?{0}", tokenParam),
                "GET",
                null);
        System.out.println(MessageFormat.format("从微信服务器获取Token返回值：{0}", result_token));
        if (StrUtil.isEffective(result_token)) {
            try {
                JSONObject result = JSONObject.parseObject(result_token);
                if ("0".equals(result.getString("errcode")) && "ok".equals(result.getString("errmsg"))) {
                    String access_token = result.getString("access_token");
                    int expires_in = result.getInteger("expires_in");
                    //
                    Record record_tokenUrlForTxl = new Record();
                    record_tokenUrlForTxl.set("ACCESS_TOKEN", access_token);
                    record_tokenUrlForTxl.set("EXPIRES_IN", expires_in);
                    record_tokenUrlForTxl.set("CATEGORY", category);
                    boolean flag = Db.save("XJ_SCRM_TOKEN", "ACCESS_TOKEN", record_tokenUrlForTxl);
                    System.out.println(MessageFormat.format("添加到本地：{0}", flag));
                } else {
                    System.out.println(MessageFormat.format("从微信服务器获取Token失败：{0}", JSONObject.toJSONString(result)));
                }
            } catch (Exception e) {
                System.out.println(MessageFormat.format("从微信服务器获取Token失败：{0}", e.getMessage()));
            }
        } else {
            System.out.println("返回值为空");
        }
        System.out.println();
    }

//    /**
//     * @Description TODO 通讯录Token
//     * @Author 李星 lixing_java@163.com
//     * @Date 2020/3/24 18:55
//     */
//    public void getTokenUrlForTxl(String tokenUrlForTxl) {
//        System.out.println("TokenTask----getTokenUrlForTxl：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//        System.out.println();
//        String result_tokenUrlForTxl = HttpHelper.sendRequestHttps(tokenUrlForTxl, "GET", null);
//        System.out.println(MessageFormat.format("getTokenUrlForTxl 从微信服务器获取Token返回值：{0}", result_tokenUrlForTxl));
//
//        if (StrUtil.isEffective(result_tokenUrlForTxl)) {
//            try {
//                JSONObject result = JSONObject.parseObject(result_tokenUrlForTxl);
//                if ("0".equals(result.getString("errcode")) && "ok".equals(result.getString("errmsg"))) {
//                    String access_token = result.getString("access_token");
//                    int expires_in = result.getInteger("expires_in");
//                    //
//                    Record record_tokenUrlForTxl = new Record();
//                    record_tokenUrlForTxl.set("ACCESS_TOKEN", access_token);
//                    record_tokenUrlForTxl.set("EXPIRES_IN", expires_in);
//                    record_tokenUrlForTxl.set("CATEGORY", 1); // TODO 通讯录
//                    boolean flag = Db.save("XJ_SCRM_TOKEN", record_tokenUrlForTxl);
//                    System.out.println(MessageFormat.format("getTokenUrlForTxl 添加到本地：{0}", flag));
//                } else {
//                    System.out.println(MessageFormat.format("getTokenUrlForTxl 从微信服务器获取Token失败：{0}", JSONObject.toJSONString(result)));
//                }
//            } catch (Exception e) {
//                System.out.println(MessageFormat.format("getTokenUrlForTxl 从微信服务器获取Token失败：{0}", e.getMessage()));
//            }
//        } else {
//            System.out.println("getTokenUrlForTxl 返回值为空");
//        }
//    }
//
//    /**
//     * @Description TODO 外部联系人Token
//     * @Author 李星 lixing_java@163.com
//     * @Date 2020/3/24 18:55
//     */
//    public void getTokenUrlForWblxr(String tokenUrlForWblxr) {
//        System.out.println("TokenTask----getTokenUrlForWblxr：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//        System.out.println();
//        String result_tokenUrlForWblxr = HttpHelper.sendRequestHttps(tokenUrlForWblxr, "GET", null);
//        System.out.println(MessageFormat.format("getTokenUrlForWblxr 从微信服务器获取Token返回值：{0}", result_tokenUrlForWblxr));
//
//        if (StrUtil.isEffective(result_tokenUrlForWblxr)) {
//            try {
//                JSONObject result = JSONObject.parseObject(result_tokenUrlForWblxr);
//                if ("0".equals(result.getString("errcode")) && "ok".equals(result.getString("errmsg"))) {
//                    String access_token = result.getString("access_token");
//                    int expires_in = result.getInteger("expires_in");
//                    //
//                    Record record_tokenUrlForWblxr = new Record();
//                    record_tokenUrlForWblxr.set("ACCESS_TOKEN", access_token);
//                    record_tokenUrlForWblxr.set("EXPIRES_IN", expires_in);
//                    record_tokenUrlForWblxr.set("CATEGORY", 2); // TODO 外部联系人
//                    boolean flag = Db.save("XJ_SCRM_TOKEN", record_tokenUrlForWblxr);
//                    System.out.println(MessageFormat.format("getTokenUrlForWblxr：{0}", flag));
//                } else {
//                    System.out.println(MessageFormat.format("getTokenUrlForWblxr 从微信服务器获取Token失败：{0}", JSONObject.toJSONString(result)));
//                }
//            } catch (Exception e) {
//                System.out.println(MessageFormat.format("getTokenUrlForWblxr 从微信服务器获取Token失败：{0}", e.getMessage()));
//            }
//        } else {
//            System.out.println("getTokenUrlForWblxr 返回值为空");
//        }
//    }
//
//    /**
//     * @Description TODO 应用消息Token
//     * @Author 李星 lixing_java@163.com
//     * @Date 2020/3/24 18:56
//     */
//    public void getTokenUrlForYgXx(String tokenUrlForYgXx) {
//        System.out.println("TokenTask----getTokenUrlForYgXx：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//        System.out.println();
//        String result_tokenUrlForYgXx = HttpHelper.sendRequestHttps(tokenUrlForYgXx, "GET", null);
//        System.out.println(MessageFormat.format("getTokenUrlForYgXx 从微信服务器获取Token返回值：{0}", result_tokenUrlForYgXx));
//
//        if (StrUtil.isEffective(result_tokenUrlForYgXx)) {
//            try {
//                JSONObject result = JSONObject.parseObject(result_tokenUrlForYgXx);
//                if ("0".equals(result.getString("errcode")) && "ok".equals(result.getString("errmsg"))) {
//                    String access_token = result.getString("access_token");
//                    int expires_in = result.getInteger("expires_in");
//                    //
//                    Record record_tokenUrlForYgXx = new Record();
//                    record_tokenUrlForYgXx.set("ACCESS_TOKEN", access_token);
//                    record_tokenUrlForYgXx.set("EXPIRES_IN", expires_in);
//                    record_tokenUrlForYgXx.set("CATEGORY", 3); // TODO 应用消息
//                    boolean flag = Db.save("XJ_SCRM_TOKEN", record_tokenUrlForYgXx);
//                    System.out.println(MessageFormat.format("getTokenUrlForYgXx：{0}", flag));
//                } else {
//                    System.out.println(MessageFormat.format("getTokenUrlForYgXx 从微信服务器获取Token失败：{0}", JSONObject.toJSONString(result)));
//                }
//            } catch (Exception e) {
//                System.out.println(MessageFormat.format("getTokenUrlForYgXx 从微信服务器获取Token失败：{0}", e.getMessage()));
//            }
//        } else {
//            System.out.println("getTokenUrlForYgXx 返回值为空");
//        }
//    }

}
